// pages/index/groupPurchase/groupOrder/groupOrder.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: '',
        // mobile:'',
    pick1: '../../../../imgs/pick1.png',
    pick2: '../../../../imgs/pick2.png',
    id1: 'active1',
    id2: 'active2',
    // mobile:'',
    // com_id:'',
    // com_price:'',
    // last_price:'',
    // num:1
    
    // mobile: 111,
    // com_id: '11',
    // com_price: 111,
    // last_price: 11,
    num: 1,
    sty:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    console.log('options',options)
    if(options.mobile){
     _this.setData({
       mobile: options.mobile
     })
     console.log(options.mobile)
    }
    
    if(options.coupon==0){
      console.log('aaaa')
      console.log(options.com_id)
      this.setData({
        com_id: options.com_id,
        com_price: options.price,
        last_price:options.price,
        price: options.price,
        mobile:options.mobile,
        coupon:options.coupon,
        groupName: options.groupName
      })
      }
      if (options.coupon==1){
        console.log('bbbb')
        let last_price = ((options.com_price)*1 - (options.amount)*1).toFixed(2);
        if (last_price<0){
          last_price = 0
        }
        _this.setData({
          coupon_name: options.coupon_name,
          amount: options.amount,
          coupon_id: options.coupon_id,
          last_price: last_price,
          num: options.num,
          com_price: options.com_price,
          coupon:1,
          price: options.price,
          com_id:options.com_id
        })
      }

      // if(options.coupon ==2){
      //   this.setData({
      //     com_id: options.com_id,
      //     com_price: options.price,
      //     last_price: options.price,
      //     price: options.price,
      //     mobile: options.mobile,
      //     coupon: options.coupon
      //   })
      // }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  phoneNum(e){
    this.setData({
      mobile:e.detail.value
    })
  },
  addNum(e){
    console.log(e)
    let _this = this;
    let num = parseInt(_this.data.num);
    console.log('num',num)
    let id = e.currentTarget.id;
    let price = Math.abs(_this.data.price);
    console.log('price', price)
    let com_price = 0;
    let last_price = Math.abs(_this.data.last_price);
    console.log('last_price', last_price)
    let coupon = _this.data.coupon;
    if(id == 'add'){
        num += 1;
        com_price = num * Math.abs(price)
        console.log('com_price',com_price)
    }else {
      if (num > 1) {
        num -= 1
      } else {
        num = num
      }
      com_price = num * Math.abs(price)
      console.log('com_price', com_price)
    }
    if(coupon == 1){
      last_price = Math.abs(com_price) - Math.abs(_this.data.amount)
    }else {
      last_price = Math.abs(com_price)
    }
    last_price = Math.floor(last_price * 100) / 100;  
    com_price = Math.floor(com_price * 100) / 100; 
    _this.setData({
      num:num,
      com_price:com_price,
      last_price: last_price
    })
  },
  submitOrder(e){
    let _this = this;
    if (!_this.data.mobile) {
      wx.showToast({
        title: '请输入电话号码',
      })
      return
    }
    _this.setData({
      active:'active'
    })
    // console.log(_this.data.com_id)
    app.wxrequest({
      url: app.globalData.baseUrl +'/api/commodity/commodity_buy',
      data:{
        com_id:_this.data.com_id,
        buyNum:_this.data.num,
        order_price: _this.data.com_price,
        session_key:app.gs(),
        phone: _this.data.mobile,
        coupons_id: _this.data.coupon_id || '',
        coupons_price: _this.data.amount || ''
      },
      success:function(res){
        wx.hideLoading();
        console.log('reeeee',res);
        _this.setData({
          id:res.data.data.id
        })
        if(res.data.code == 200){
          // wx.showToast({
          //   title: '下单成功',
          // })
          app.wxrequest({
            url: app.globalData.baseUrl + '/api/pay/payByOrder',
            data: {
              session_key: app.gs(),
              order_id: _this.data.id,
              type: _this.data.sty,
              form_id:e.detail.formId
            },
            success: function (res) {
              wx.hideLoading()
              console.log(res);
              if (res.data.message =='支付成功'){
                wx.showToast({
                  title: '支付成功',
                })
                setTimeout(function(){
                  wx.redirectTo({
                    url: '../groupPurchase',
                  })
                },2000)
              }
              if (_this.data.sty == 1) {
                wx.requestPayment({
                  timeStamp: res.data.data.timeStamp,
                  nonceStr: res.data.data.nonceStr,
                  package: res.data.data.package,
                  signType: res.data.data.signType,
                  paySign: res.data.data.paySign,
                  success(res) {
                    console.log(res)
                    if (res.data.message == 201) {
                      if(_this.data.coupon==1){
                        setTimeout(function(){
                          wx.navigateBack({
                            delta:4
                          })
                        },2000)
                      }else {
                        setTimeout(function () {
                          wx.redirectTo({
                            url: '../groupPurchase',
                          })
                        }, 2000)
                      }
                     
                    } else if (res.data.message == 400) {
                      wx.showToast({
                        title: '支付失败',
                      })
                    }
                  }
                })
              } else if (_this.data.sty == 2) {
                if (res.data.message == 402) {
                  wx.showToast({
                    title: '会员卡余额不足',
                  })
                } else if (res.data.message == '支付成功') {
                  wx.showToast({
                    title: '支付成功',
                    success() {
                      if(_this.data.coupon==1){
                        setTimeout(function(){
                          wx.navigateBack({
                            delta: 2,
                          })
                        },2000)
                      }else {
                        setTimeout(function () {
                          wx.navigateBack({
                            delta: 1
                          })
                        }, 2000)
                      }
                     
                    }
                  })
                }
              }
            }
          })
        }
        
      }
    })
  },
  chooseCoupon(e){
    let _this = this;
    console.log(_this.data.com_id)
    wx.redirectTo({
      url: '../../wineShop/orderPay/chooseCoupon/chooseCoupon?sty=2' + '&com_id=' + _this.data.com_id + '&com_price=' + _this.data.com_price + '&mobile=' + _this.data.mobile + '&num=' + _this.data.num + '&com_id=' + _this.data.com_id + '&price=' + _this.data.price,
    })
  },
  payStyle(e) {
    // console.log(e)
    let _this = this;
    if (e.currentTarget.id == 'active2') {
      _this.setData({
        pick2: '../../../../imgs/pick1.png',
        pick1: '../../../../imgs/pick2.png',
        sty: e.currentTarget.dataset.sty
      })
    } else {
      _this.setData({
        pick1: '../../../../imgs/pick1.png',
        pick2: '../../../../imgs/pick2.png',
        sty: e.currentTarget.dataset.sty
      })
    }
  }
 
})